package censorboty;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.google.wave.api.Event;
import com.google.wave.api.FormView;



public class CensorWords {

	private DocumentBuilder builder = null;
	private Document doc = null;
	
	private ArrayList<CensorWord> cencorWords = new ArrayList<CensorWord>();
	public CensorWords(){
		
		
		try {
			builder  = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			doc = builder.parse("http://censorboty.appspot.com/imgs/swearWords.xml");
			NodeList songs = doc.getElementsByTagName("word");
			for(int i=0;i<songs.getLength();i++){
				cencorWords.add(new CensorWord(songs.item(i).getTextContent()));
			}
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	public ArrayList<CensorWord> getCencorWords() {
		return cencorWords;
	}
	public void addWord(Event e){
		FormView fm=e.getBlip().getDocument().getFormView();
		String inp=fm.getFormElement("input").getValue();
		CensorWord newW=new CensorWord(inp.toLowerCase());
		if(!cencorWords.contains(newW)){
			this.cencorWords.add(newW);
		}else{
			return ;
		}
	}
	public void removeWord(Event e){
		FormView fm=e.getBlip().getDocument().getFormView();
		String inp=fm.getFormElement("input").getValue();
		CensorWord newW=new CensorWord(inp.toLowerCase());
		if(cencorWords.contains(newW)){
			this.cencorWords.remove(newW);
		}else{
			return ;
		}
	}
}
